<template>
		
	<view class="login">
		<form @submit="formSubmit">
		<view class="content-wrapper">
			<view class="title">
				<!-- <h1>欢迎登录</h1> -->
			</view>
			<view style="text-align: center;" >
				<image src="../../static/panpan.jpg"  style="width: 160rpx;height: 160rpx;"></image>
			</view>
			
		
				<view class="login-form">
					<view class="login-form-items">
						<view class="login-form-items-title"> 
						<image src="../../static/login.png" style="width: 50rpx;height: 50rpx;"></image></view>
						<input type="text" class="login-input" name="username" placeholder="用户名/邮箱/手机号" />
					</view>
				
					<view class="login-form-items">
						<view class="login-form-items-title"><image src="../../static/pwd.png" style="width: 50rpx;height: 50rpx;"></image></view>
						<input type="password" name="password" class="login-input"    placeholder="请输入登录密码" />
					</view>
				           
				</view>
			
		</view>
		<div class="submit-wrapper">
			<button type="primary" form-type="submit" class="login-btn">登录</button>
			
			<!-- <button type="primary" @click="wxLogin"  style="margin-top: 35rpx;background-color: #4CD964;">微信一键登录</button> -->
		</div>
		
		</form>
		<button type="warn" @click="register" style="margin: 20rpx 20rpx 20rpx 20rpx;" class="login-btn">注册</button>
		
	</view>
</template>

<script>
		import http from '../../common/request.js'
		import {openComDB,closeComDB,executeSQL} from '../../common/sqlite.js'
	export default {
		mounted() {
			var a = document.getElementsByClassName('uni-page-head-hd')[0];
			if(a){
				a.style.display = 'none';
			}
			this.initHome()
		},
		data() {
			return {
			};
		},
		methods:{
			initHome:function(){
			let user=uni.getStorageSync('userKey')
			if(user.id!=undefined&&user.id!=null&&user.id!=''){
				uni.switchTab({
					url: '/pages/index/chat/chat',
					animationType: 'pop-in',
					animationDuration: 200
				}); 
			 }
			},
			
			
			async login(formdata){
				
				const {data:res} =await uni.$http.post('/sys_user/login_mobile',formdata);
				
				console.log('-----------res-----,',res);
				if (res.code == 0) {
					let user = res.data;
					
						uni.setStorageSync('userKey',user)
						uni.setStorageSync('authorization',user.authorization)
						uni.switchTab({
							url: '/pages/index/chat/chat',
							animationType: 'pop-in',
							animationDuration: 200
						}); 
					}else if(res.data.code ==-1){
					
						uni.showToast({
							  	title:res.data.msg
						})
					}
			},
			
			
			async loginPre(formdata) {
							const res = await http.request({
								url: '/sys_user/login_mobile',
								method:'POST',
								data:formdata
							})
						    if(res.data.code==0){
								let user = res.data.data
							
								uni.setStorageSync('userKey',user)
								uni.setStorageSync('authorization',user.authorization)
								uni.switchTab({
									url: '/pages/index/chat/chat',
									animationType: 'pop-in',
									animationDuration: 200
								}); 
							}else if(res.data.code ==-1){

								uni.showToast({
									  	title:res.data.msg
								})
							}
						},
			
			
				formSubmit: function(e) {
					              if(e.detail.value.username==''||e.detail.value.username==undefined){
									  uni.showToast({
									  	title:'用户名不能为空！'
									  })
									  return false
								  }
								  if(e.detail.value.password==''||e.detail.value.password==undefined){
								  									  uni.showToast({
								  									  	title:'密码不能为空！'
								  									  })
								  									  return false
								  }
								let formdata = e.detail.value
								this.login(formdata)
								
								
							},
							formReset: function(e) {
								console.log('清空数据')
							},
							register(){
								uni.navigateTo({
									url:'register/register'
								})
							},
							wxLogin(){
								uni.login({
								  provider: 'weixin',
								  success: function (loginRes) {
								    console.log(loginRes.authResult);
								    // 获取用户信息
									uni.showLoading({title: '登录中...',mask:true});
								    uni.getUserInfo({
								      provider: 'weixin',
								      success: function (infoRes) {
								        console.log('用户昵称为：' + infoRes.userInfo.nickName);
								        console.log('用户：' + JSON.stringify(infoRes.userInfo));
										uni.request({
											url: 'http://api.huiyishou.xyz/v1/sys_user/login_mobile',
											method: 'POST',
											data: infoRes.userInfo,
											success: (res) => {
												if(res.data.code==0){
													let user = res.data.data
												
													uni.setStorageSync('userKey',user)
													uni.setStorageSync('authorization',user.authorization)
													console.log('user.username',user.username);
													if(undefined==user.username){
														console.log('1111')
														//未绑定用户名
														uni.navigateTo({
															url:'/pages/index/my/bind/bind'
														})
													}else{
														uni.switchTab({
															url: '/pages/index/chat/chat',
															animationType: 'pop-in',
															animationDuration: 200
														});
													}
													 
													uni.hideLoading();
												}else if(res.data.code ==-1){
												    uni.hideLoading();
													uni.showToast({
														  	title:res.data.msg
													})
												}
											},
											fail:(err)=> {
												uni.hideLoading();
												uni.showToast({
													title: "请求接口失败"
												})
												reject(err)
											}
										})
								      }
								    });
								  }
								});
							}
						}
					}
</script>

<style lang="scss">
	page {
		background: #F4F5F6;
	}

	.login {
		.content-wrapper {
			height: 99%;
			width: 100%;

			.title {
				margin-top: 10rpx;
				width: 100%;
				margin-bottom: 10px;

				h1 {
					border: 0px;
					width: 50%;
					margin: 0 auto;
					text-align: center;
					border-bottom: 1px solid #E3E3E3;
					line-height: 50px;
					font-size: 17px;
					overflow: hidden;
				}
			}

			.login-form {
				margin: 20px 10px 20px 15px;
				background: #FFFFFF;

				.login-form-items {
					padding: 15px 10px;
					border-bottom: 1px solid #F3F4F5;
					position: relative;
					display: -webkit-flex;
					display: flex;

					.login-form-items-title {
						width: 10%;
						line-height: 22px;
						flex-shrink: 0;
					}

					.login-input {
						width: 100%
					}
				}
			}
		}

		.submit-wrapper {
			padding: 10px;
			padding-top: 10px;
		}

	}
</style>

